using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using EditWebSite.Logics;
using EditWebSite.Controls;


namespace EditWebSite.Presentation
{
    internal partial class FrmQuery : Form
    {
        DatabaseHelper _Helper;
        EditWebSite.Helper _hlp;

        public FrmQuery(DatabaseHelper Helper)
        {
            InitializeComponent();

            _Helper = Helper;

            _hlp = new Helper(Helper);


            tabQuery.TabPages.Clear();
            tsbNuova_Click(this, new EventArgs());

            if (Program.Utente.Livello == 5)
            {
                tsbNuovoUtente.Visible = true;
            }
            else
            {
                tsbNuovoUtente.Visible = false;
            }
        }



        private void copiaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                Clipboard.SetText(tb.TxtSql.SelectedText);
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void selezionaTuttoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                tb.TxtSql.SelectAll();
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void cancellaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                tb.TxtSql.SelectedText = string.Empty;
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void incollaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                tb.TxtSql.SelectedText = Clipboard.GetText();
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void tagliaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                Clipboard.SetText(tb.TxtSql.SelectedText);
                tb.TxtSql.SelectedText = string.Empty;
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void copiaInCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }
                string testo = _Helper.GetTestoFormattato(tb.TxtSql.SelectedText);
                Clipboard.SetText(testo);

            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void tsbNuova_Click(object sender, EventArgs e)
        {
            TabPage tab = new TabPage("Query " + (tabQuery.TabPages.Count + 1));
            CustomTabPage cus = new CustomTabPage();
            cus.TxtSql.SetHelper(_hlp);
            cus.Dock = DockStyle.Fill;
            cus.TxtSql.ContextMenuStrip = cmMenu;
            cus.EseguiQuery += new EventHandler(cus_EseguiQuery);
            tab.Controls.Add(cus);
            tabQuery.TabPages.Add(tab);
            tabQuery.SelectedTab = tab;
        }

        void cus_EseguiQuery(object sender, EventArgs e)
        {
            tsbEsegui_Click(sender, e);
        }

        private void tsbRimuovi_Click(object sender, EventArgs e)
        {
            if (tabQuery.SelectedTab != null)
                tabQuery.TabPages.Remove(tabQuery.SelectedTab);
        }

        private void tsbRimuoviTutte_Click(object sender, EventArgs e)
        {
            if (Messaggi.Confirm("Sei sicuro di voler elinare tutte le tab aperte?"))
            {
                tabQuery.TabPages.Clear();
            }
        }

        private void tsbEsegui_Click(object sender, EventArgs e)
        {
            try
            {
                CustomTabPage tb = tabQuery.SelectedTab.Controls[0] as CustomTabPage;
                if (tb == null)
                {
                    return;
                }

                if (_Helper.SqlIsSelect(tb.TxtSql.Text))
                {
                    DataTable dt = _Helper.GetDataTable(tb.TxtSql.Text);
                    tb.Dgv.DataSource = dt;
                }
                else
                {
                    int numeroRighe = _Helper.ExecuteQueryWithConfirm(tb.TxtSql.Text);
                    if (numeroRighe > -1)
                    {
                        Messaggi.Messagge("Sono state modificate " + numeroRighe + " righe.");
                    }
                }
            }
            catch (Exception ex)
            {
                #region LOGIC
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void tsbTabelle_Click(object sender, EventArgs e)
        {
            try
            {
                FrmTabelle fTab = new FrmTabelle(_Helper);
                fTab.Show();
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                MessageBox.Show(ex.Message);
                #endregion
            }
        }

        private void tsbNuovoUtente_Click(object sender, EventArgs e)
        {
            try
            {
                FrmNuovoUtente fNew = new FrmNuovoUtente();
                if (fNew.ShowDialog() == DialogResult.OK)
                {
                    Authentication.Authentication auth = new EditWebSite.Authentication.Authentication();

                    if (auth.NuovoLogin(fNew.Username, fNew.Password, fNew.Livello, Program.Utente.Username))
                    {
                        Messaggi.Messagge("Nuovo utente inserito.");
                    }
                    else
                    {
                        Messaggi.Error("Utente NON inserito.");
                    }
                }
                fNew.Dispose();
                fNew = null;
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }




    }
}